// If you are in "stata" folder, type:
use ../data/data_final.dta, clear


// Keep only electoral years (1994, 1998, 2002, 2006, 2010, 2014) 
drop if elecyear != 1
// Set time series and cross section
xtset uf_cod year
// Generate lags
**  Incumbent vote in the previous election
gen l4vote = l4.elecvote
**  Previous incumbent vote in the previous election
gen l4votesh = l4.vote_share
// Generate interaction
** State unemployment and industry+services 
gen un_ind1 = ind_ser*unempch_uf
** Benchmark unemployment and industry+services 
gen un_ind2 = ind_ser*unempch_rel
** State unemployment and taxes
gen un_tax1 = tax*unempch_uf
** Benchmark unemployment and taxes
gen un_tax2 = tax*unempch_rel
// Keep only included observations
drop if included != 1


// FIGURE 3:(1) State unemployment change as marginal variable
// Adapted from Matt Golder
reg vote_share unempch_uf ind_ser un_ind1 l4vote incumbent tax if included == 1, vce(cluster uf)

gen MV=ind_ser 
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1]
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
scalar list b1 b2 b3 varb1 varb2 varb3 covb1b3 covb2b3
* This calculates the marginal effect of X on Y:
gen conb=b1+b3*MV
* This calculates the standard error of the estimate of the effect that we just calculated:
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) 
* This generates the upper and lower bounds although these should be modified according to the
* number of degrees of freedom if you do not have as many as we have in this model:
gen a=1.66*conse
gen upper=conb+a
gen lower=conb-a
gen where=-10
gen pipe = "|"
egen tag = tag(ind_ser)
gen yline = 0

sort MV
graph twoway hist ind_ser,  percent color(gs14) yaxis(2) /*
*/		  ||   line conb   MV, clwidth(medium) clcolor(blue) clcolor(black) /*
*/        ||   line upper  MV, clpattern(dash) clwidth(thin) clcolor(black) /*
*/        ||   line lower  MV, clpattern(dash) clwidth(thin) clcolor(black) /*
*/        ||   scatter where ind_ser, plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) /*
*/		  ||   line yline MV,  clwidth(thin) clcolor(black) clpattern(solid) /*  
*/        || ,   /*
*/             legend(col(1) order(1 2) label(1 "Marginal Effect of Unemployment ")  /*
*/                                      label(2 "") /*
*/                                      label(3 " ")) /*
*/             yline(0, lcolor(black))   /*
*/             title("", size(4)) /*
*/             subtitle(" " "" " ", size(3)) /*
*/             xtitle( "Industry + Services", size(3)  ) /*
*/             yscale(noline alt) /*
*/             yscale(noline alt axis(2))/*
*/	           xscale(noline) /*
*/			   legend(off) /* 
*/             ytitle("Marginal Effect of Unemployment Change", size(3)) /*
*/			   ytitle("Percent of observations", size(3) axis(2))/*
*/		       note("") /*
*/             scheme(s2mono) graphregion(fcolor(white)) /*
*/ xlabel(40 50 60 70 80 90, nogrid labsize(2)) /*
*/ ylabel(-10 -5 0 5 10, axis(1) nogrid labsize(2)) /*
*/ ylabel(0 5 10 15, axis(2) nogrid labsize(2))


graph export "../figures/figure3.png", width(2000) replace

drop MV conb conse a upper lower where pipe tag yline
